package disorderly;

import java.util.Map;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class Attendance implements Comparable<Attendance>, Cloneable {
  public String ClassID;
  public String Name;
  public String ClassName;
  public int InviteStatus = 0;
  public transient Integer Attendance = 0;
  public transient Integer Attendance2w = 0;
  public transient Integer Attendance2m = 0;
  public transient Map<Attendance, Integer> pastEvents;
  public transient Map<Attendance, Integer> pastEvents2w;
  public transient Map<Attendance, Integer> pastEvents2m;

  public int getPastRaidedCount() {
    Integer pastRaidedCount = pastEvents.get(this);
    return pastRaidedCount == null ? 0 : pastRaidedCount;
  }

  public Double getAttendancePercentage() {
    if (pastEvents.get(this) == null || Attendance == null) {
      return 0.0;
    }
    return Math.round((Attendance / (pastEvents.get(this) * Status.CONFIRMED.getAttendanceValue() * 1.0)) * 1000) / 10.0;
  }

  public Double getAttendancePercentage2w() {
    if (pastEvents2w.get(this) == null || Attendance2w == null) {
      return 0.0;
    }
    return Math.round((Attendance2w / (pastEvents2w.get(this) * Status.CONFIRMED.getAttendanceValue() * 1.0)) * 1000) / 10.0;
  }

  public Double getAttendancePercentage2m() {
    if (pastEvents2m.get(this) == null || Attendance2m == null) {
      return 0.0;
    }
    return Math.round((Attendance2m / (pastEvents2m.get(this) * Status.CONFIRMED.getAttendanceValue() * 1.0)) * 1000) / 10.0;
  }

  @Override
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj, "InviteStatus");
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this, "InviteStatus");
  }

  @Override
  public String toString() {
    return Name + ": " + InviteStatus;
  }

  @Override
  public int compareTo(Attendance o) {
    if (InviteStatus == 0 && o.InviteStatus != 0) {
      return 1;
    }
    if (InviteStatus != 0 && o.InviteStatus == 0) {
      return -1;
    }
    if (pastEvents2w != null && o.pastEvents2w != null && Attendance2w != null && o.Attendance2w != null && !Attendance2w.equals(o.Attendance2w) && !o.getAttendancePercentage2w().equals(getAttendancePercentage2w())) {
      return o.getAttendancePercentage2w().compareTo(getAttendancePercentage2w());
    }
    if (pastEvents != null && o.pastEvents != null && Attendance != null && o.Attendance != null && !Attendance.equals(o.Attendance) && !o.getAttendancePercentage().equals(getAttendancePercentage())) {
      return o.getAttendancePercentage().compareTo(getAttendancePercentage());
    }
    if (Attendance != null && o.Attendance != null && !o.Attendance.equals(Attendance)) {
      return o.Attendance.compareTo(Attendance);
    }
    return Name.compareTo(o.Name);
  }

  @Override
  protected Attendance clone() {
    Attendance res = new Attendance();
    res.ClassID = ClassID;
    res.Name = Name;
    res.ClassName = ClassName;
    res.InviteStatus = InviteStatus;
    return res;
  }
}
